

#ifndef __CORTISOL_RGB_VIDEO_TEXTURE_H
#define __CORTISOL_RGB_VIDEO_TEXTURE_H

#include "ITextureUpdater.h"
#include "VideoRGB.h"

#include <string>

#define GL_GLEXT_PROTOTYPES 1
#include <GL/GL.h>
#include <wgl/glext.h>
#include <wgl/wglext.h>

class GLVideoTextureRGB
	:	public VideoRGB,
		public ITextureUpdater
{
	GLuint pbo;
	GLuint tex;

public:

	GLVideoTextureRGB(const std::string &file, int times)
		:	VideoRGB(file,times)
	{
		Initialise();
	}

	virtual ~GLVideoTextureRGB()
	{
		Dispose();
	}

	 void UpdateTexture() {

		const GLvoid* data = rgbOutputFrame->data[0];
		GLsizeiptr size = rgbOutputFrame->linesize[0] * GetHeight();
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
		glBufferData(GL_PIXEL_UNPACK_BUFFER, size, NULL, GL_STREAM_DRAW);
		void* pboData = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
		memcpy(pboData, data, size);
		glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
		glBindTexture(GL_TEXTURE_2D, tex);
		glTexSubImage2D(GL_TEXTURE_2D,0,0,0,GetWidth(),GetHeight(),GL_BGRA,GL_UNSIGNED_BYTE, static_cast<const GLvoid*>(0));
		glBindTexture(GL_TEXTURE_2D, 0);
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
	 }

private:

	void Initialise() {

		glGenBuffers(1, &pbo);
		glGenTextures(1, &tex);
	}

	void Dispose() {

		glDeleteTextures(1,&tex);
		glDeleteBuffers(1, &pbo);

	}
};

#endif /*** __CORTISOL_RGB_VIDEO_TEXTURE_H ***/